﻿using System;
using System.IO;
using System.Windows.Forms;
using Ecng.Serialization;
using Sample.TradingClasses.RobotsData;
using StockSharp.Algo.Storages;
using StockSharp.BusinessEntities;

namespace Sample.TradingClasses
{
    [Serializable]
	public class ssXmlStorage : IPersistable, IRobotStartXmlInfo
	{
		public string Stroka1 { get; set; }
		public string FileToSave { get; set; }
		public string FileToRead { get; set; }

        public DateTime RobotDateTimeCreating { get; set; }
        public RobotType RobotType { get; set; }
        public ONOFF RobotMode { get; set; }
        public string RobotName { get; set; }
        public string RobotFolderNameXML { get; set; }
        public string RobotFileNameXML { get; set; }
        public string RobotFullNameXML { get; set; }
        public string RobotFolderNameDB { get; set; }
        public string RobotFileNameDB { get; set; }
        public string RobotFullNameDB { get; set; }
        public Portfolio RobotPF { get; set; }

		public ssXmlStorage(String fileToStore)
		{
            FileToSave = fileToStore;
		}

       

		public void Save(SettingsStorage settings)
		{
			string toShow;

			if (FileToSave.Length > 0)
			{
			    if (!File.Exists(FileToSave))
			    {
			        Create(FileToSave);
			    }
                settings.SetValue("RobotDateTimeCreating", RobotDateTimeCreating);
                settings.SetValue("RobotName", RobotName);
                settings.SetValue("RobotType", RobotType);
                settings.SetValue("RobotMode", RobotMode);
                settings.SetValue("RobotFullNameXML", RobotFullNameXML);
                settings.SetValue("RobotFullNameDB", RobotFullNameDB);

		    new XmlSerializer<SettingsStorage>().Serialize(settings, FileToSave); //сериализуем наши настройки
//            new XmlSerializer<SettingsStorage>().Serialize(settings, FileToSave); //сериализуем наши настройки
			}
			else
			{
				toShow = "XML-File's name is wrong: " + FileToSave;
				MessageBox.Show(toShow);
			}
		}

		public void Load(SettingsStorage settings)
		{
		    string toShow;

            if (File.Exists(FileToRead))
		    {
//                settings = XmlSerializer<SettingsStorage>().Deserialize(FileToRead);
      //         _basicStrategy.Load(settingsStorage);
  //          new XmlSerializer<SettingsStorage>().Deserialize(FileToSave, ); //deсериализуем наши настройки
		    //        Stroka1 = settings.GetValue<string>("SomeStringInformation");
		    RobotDateTimeCreating = settings.GetValue<DateTime>("RobotDateTimeCreating");
		    RobotName = settings.GetValue<string>("RobotName");
		    RobotType = settings.GetValue<RobotType>("RobotType");
            RobotMode = settings.GetValue<ONOFF>("RobotMode");
		    RobotFullNameXML = settings.GetValue<string>("RobotFullNameXML");
		    RobotFullNameDB = settings.GetValue<string>("RobotFullNameDB");
		    }


		}


        /*
         

		public void Save(SettingsStorage settings)
		{
			string toShow;

			if (FileToSave.Length > 0)
			{
			    if (!File.Exists(FileToSave))
			    {
			        Create(FileToSave);
			    }
                settings.SetValue("RobotDateTimeCreating", RobotDateTimeCreating);
                settings.SetValue("RobotName", RobotName);
                settings.SetValue("RobotType", RobotType);
                settings.SetValue("RobotFullNameXML", RobotFullNameXML);
                settings.SetValue("RobotFullNameDB", RobotFullNameDB);
		    new XmlSerializer<SettingsStorage>().Serialize(settings, FileToSave); //сериализуем наши настройки
//            new XmlSerializer<SettingsStorage>().Serialize(settings, FileToSave); //сериализуем наши настройки
			}
			else
			{
				toShow = "XML-File's name is wrong: " + FileToSave;
				MessageBox.Show(toShow);
			}
		}

		public void Load(SettingsStorage settings)
		{
		    string toShow;

		    if (File.Exists(FileToSave))
		    {
 //               settings = XmlSerializer<SettingsStorage>().Deserialize(FileToSave);
      //         _basicStrategy.Load(settingsStorage);
  //          new XmlSerializer<SettingsStorage>().Deserialize(FileToSave, ); //deсериализуем наши настройки
		    //        Stroka1 = settings.GetValue<string>("SomeStringInformation");
		    RobotDateTimeCreating = settings.GetValue<DateTime>("RobotDateTimeCreating");
		    RobotName = settings.GetValue<string>("RobotName");
		//    RobotType = settings.GetValue<RobotType>("RobotType");
		    RobotFullNameXML = settings.GetValue<string>("RobotFullNameXML");
		    RobotFullNameDB = settings.GetValue<string>("RobotFullNameDB");
		    }

        
		}

         */

		public void Create(string StorageFile)
		{
						try
			{
				var writer = new StreamWriter(StorageFile);
				//var writer= new XmlTextWriter("parameters.xml",Encoding.UTF8);//некрасивро в XML
				writer.Flush();
				writer.Close();

			}
			catch (Exception exception)
			{
				MessageBox.Show(exception.ToString());
			}

		}
	}
}

